package cn.tedu.mall.front.mapper;

import cn.tedu.mall.pojo.front.entity.FrontStockLog;
import cn.tedu.mall.pojo.product.vo.SkuStandardVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface FrontSkuMapper {
    @Select("select * from pms_sku where spu_id=#{spuId}")
    List<SkuStandardVO> selectSkusBySpuId(@Param("spuId") Long spuId);

    @Select("select * from pms_sku where id=#{skuId}")
    SkuStandardVO selectSkuById(@Param("skuId") Long skuId);

    @Update("update pms_sku set stock=stock-#{quantity} where id=#{skuId} " +
            "and stock > #{quantity}")
    int decrStock(@Param("skuId") Long skuId, @Param("quantity") Integer quantity);

    @Insert("insert into pms_stock_log " +
            "(sku_id,order_sn,quantity,gmt_create,gmt_modified) values " +
            "(#{skuId},#{orderSn},#{quantity},#{gmtCreate},#{gmtModified})")
    void insertStockLog(FrontStockLog stockLog);

    @Select("select * from pms_stock_log where order_sn=#{sn}")
    List<FrontStockLog> selectStockLogBySn(@Param("sn") String sn);

    @Update("update pms_sku set stock=stock+#{quantity} where id=#{skuId}")
    void incrStock(@Param("skuId") Long skuId, @Param("quantity") Integer quantity);
}
